﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class represents a collection of in indices.
    /// </summary>
    [Serializable]
    public class IndexCollection : AbstractCollection
    {
        /// <summary>
        /// The list of indices.
        /// </summary>
        private readonly List<int> indexList;

        /// <summary>
        /// Initializes a new instance of the <see cref="IndexCollection"/> class.
        /// </summary>
        public IndexCollection()
        {
            this.indexList = new List<int>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="IndexCollection"/> class.
        /// </summary>
        /// <param name="list">The list of indices to clone.</param>
        public IndexCollection(List<int> list)
        {
            this.indexList = list;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="IndexCollection"/> class.
        /// </summary>
        /// <param name="list">An array of indices.</param>
        public IndexCollection(int[] list)
        {
            if (list == (int[]) ((object) null))
            {
                throw new ArgumentNullException("list");
            }

            this.indexList = new List<int>();

            foreach (int i in list)
            {
                this.indexList.Add(i);
            }
        }

        /// <summary>
        /// Gets the list of the indices.
        /// </summary>
        /// <value>The list of the indices.</value>
        public List<int> Value
        {
            get { return indexList; }
        }

        /// <summary>
        /// Counts the indices in the collection.
        /// </summary>
        /// <value>The number of indices in the collection.</value>
        public int Count
        {
            get { return indexList.Count; }
        }

        /// <summary>
        /// Copies the current instance.
        /// </summary>
        /// <returns>A copy of the current instance.</returns>
        public IndexCollection Copy()
        {
            return new IndexCollection(this.ToArray());
        }

        /// <summary>
        /// Checks if a specified value in the collection exists.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>True if the specified value exists in the collection otherwise, false.</returns>
        public bool Exist(int value)
        {
            foreach (int i in this.indexList)
            {
                if (i == value)
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Adds the specified value to the collection.
        /// </summary>
        /// <param name="value">The value to add.</param>
        public void Add(int value)
        {
            this.indexList.Add(value);
        }

        /// <summary>
        /// Inserts a index at the specified index.
        /// </summary>
        /// <param name="index">The specified position.</param>
        /// <param name="value">The value to insert.</param>
        public void Insert(int index, int value)
        {
            this.indexList.Insert(index, value);
        }

        /// <summary>
        /// Removes all items from the current collection.
        /// </summary>
        public void Clear()
        {
            this.indexList.Clear();
        }

        /// <summary>
        /// Gets the value in the collection at a specified position.
        /// </summary>
        /// <param name="index">The specified position of the value.</param>
        /// <returns>The value at the specified position.</returns>
        public int GetElementAt(int index)
        {
            return this.indexList[index];
        }

        /// <summary>
        /// Removes an element at a specified position.
        /// </summary>
        /// <param name="index">The specified position of the element that should be removed.</param>
        public void RemoveAt(int index)
        {
            this.indexList.RemoveAt(index);
        }

        /// <summary>
        /// Sorts this collection.
        /// </summary>
        public void Sort()
        {
            this.indexList.Sort();
        }

        /// <summary>
        /// Compares the current collection to another index collection. This method compares the
        /// values of the collection. The position of the elements is not important.
        /// </summary>
        /// <param name="collection">The collection.</param>
        /// <returns>true if the collections are even otherwise, false.</returns>
        public bool CompareTo(IndexCollection collection)
        {
            if (collection == (IndexCollection) null)
            {
                throw new ArgumentNullException("collection");
            }

            if (this.Count != collection.Count)
            {
                return false;
            }

            IndexCollection tempuri = this.Copy();

            tempuri.Sort();
            collection.Sort();

            for (int i = 0; i < tempuri.Count; i++)
            {
                if (tempuri.GetElementAt(i) != collection.GetElementAt(i))
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first IndexCollection.</param>
        /// <param name="b">The second IndexCollection.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(IndexCollection a, IndexCollection b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            for (int i = 0; i < a.Count; i++)
            {
                if (a.GetElementAt(i) != b.GetElementAt(i))
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first IndexCollection.</param>
        /// <param name="b">The second IndexCollection.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(IndexCollection a, IndexCollection b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Gets or sets the <see cref="System.Int32"/> at the specified index.
        /// </summary>
        /// <value>The index value.</value>
        public int this[int index]
        {
            get { return this.indexList[index]; }
            set { this.indexList[index] = value; }
        }

        /// <summary>
        /// Gets the enumerator.
        /// </summary>
        /// <returns>The enumerator of the current iteration.</returns>
        public IEnumerator<int> GetEnumerator()
        {
            for (int i = 0; i < this.indexList.Count; i++)
            {
                yield return this.indexList[i];
            }
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Converts the current collection to an integer array.
        /// </summary>
        /// <returns>The converted integer array.</returns>
        public int[] ToArray()
        {
            return this.indexList.ToArray();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            String result = "{";

            foreach (int i in this.indexList)
            {
                result += i.ToString() + "; ";
            }

            result = result.Trim();
            result += "}";

            return result;
        }
    }
}